package br.com.vacinne;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import br.com.vacinne.Entity.UsuarioEntity;
import br.com.vacinne.dao.DB;

public class UsuarioAdapter extends BaseAdapter{
	private Context context;
	private List<UsuarioEntity> list;
	
	public UsuarioAdapter(Context context, List<UsuarioEntity> list){
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0).getIdUsuario();
	}

	@Override
	public View getView(int position, View arg1, ViewGroup arg2) {
		final int auxPosition = position;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.usuario, null);
		
		TextView tv = (TextView) layout.findViewById(R.id.nome);
		tv.setText(list.get(position).getNome());
		
		
		Button editarBt = (Button) layout.findViewById(R.id.editar);
		editarBt.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, NewUserActivity.class);
				intent.putExtra("nome", list.get(auxPosition).getNome());
				intent.putExtra("email", list.get(auxPosition).getEmail());
				intent.putExtra("idUsuario", list.get(auxPosition).getIdUsuario());
				context.startActivity(intent);
			}
		});
		
		Button deletarBt = (Button) layout.findViewById(R.id.deletar);
		deletarBt.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				DB bd = new DB(context);
				bd.delete(list.get(auxPosition), 2);
				
				layout.setVisibility(View.GONE);
			}
		});
		
		return layout;
	}

}
